package org.dromara.system.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HtmlToMapConverter {
    public static List<Map> handleHtmlTable(String html) {
        Document doc = Jsoup.parseBodyFragment(html);
        // 选择表格元素
        Element table = doc.select("table").first();
        // 获取表格的行
        Elements rows = table.select("tr");
        // 遍历每一行
        List<Map> list  = new ArrayList<>();
        for (Element row : rows) {
            Map<Object,Object> map = new HashMap<>();
            Elements cols = row.select("td"); // 获取该行的列
            for(int i =0 ;i<cols.size();i++){
                // 提取并打印每列的数据
                map.put(i, cols.get(i).text());
            }
            list.add(map);
        }
        return list;
    }
}
